<?php
class DBMEntityFinder{
	public static function select(DBMCommand $command){
		// get the main entity and the class
		$entity = $command->getMainEntity();
		$class = $entity->getClass();
		
		// set the operation of the command
		$command->setOperation(DBMOperations::SELECT);
		
		// create the collection
		$collection = array();
		
		// run
		$resultset = $this->connection->executeSelect($command);
		
		// search the resultset
		foreach($resultset as $result){
			// create the instance of the object
			$object = new $class();
			
			// get the data on the result
			$values = $result->getAll();
			
			// search the data
			foreach($values as $alias => $value){
				// get the attribute by the alias
				$attribute = $entity->getAttributeByAlias($alias);
				
				// check if the attribute have been found
				if(is_null($attribute)){
					// go to the next attribute
					continue;
				}
				
				// get the name of the setter method
				$setMethod = DBMReflectionHelper::findSetter($class, $attribute->getName());
				
				// set the value
				$object->$setMethod($value);
			}
			
			// load the dependences
			$object = $this->loadRelationships($object);
			
			// store
			$collection[] = $object;
		}
		
		// return 
		return $collection;
	}
} 
?>